<?php
declare (strict_types=1);

namespace app\common\controller;

use app\common\model\Relation;
use app\common\model\User;
use app\common\model\UserNode;
use app\common\tool\account\Account;
use think\facade\Cache;
use think\facade\Event;

class Bonus
{
    /**
     * 奖金入账
     */
    public static function bonusEntry($uid, $count, $order)
    {
        try {
            $system = Cache::get('system');
            UserNode::with('user')->where('new_uid', $uid)
                ->whereIn('algebra', [$count, $count+1])->order('algebra', 'asc')->select()
                ->each(function ($item) use ($system, $count, $order){
                    if($item->algebra == $count){
                        $amount1 = round($system['product_bonus_amount'], $system['algebra1_bonus'], 2);
                        Account::getInstance()->basics($item->uid, 'bonus', $amount1, $count.'层奖励', '+')->setOrder($order->id)->createBonus($order->uid, 1);
                    }elseif($item->algebra == $count+1){
                        $amount1 = round($system['product_bonus_amount'], $system['algebra2_bonus'], 2);
                        Account::getInstance()->basics($item->uid, 'bonus', $amount1, ($count+1).'层奖励', '+')->setOrder($order->id)->createBonus($order->uid, 1);
                    }
                });
            return ['code'=>1, 'msg'=>'success'];
        } catch (\Exception $e) {
            return ['code'=>-1, 'msg'=>$e->getMessage()];
        }
    }

}